package com.aqie.arithmetic.math;

import edu.princeton.cs.algs4.Bag;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

/**
 * 统计数据 Bag类使用
 */
public class Stats {
    /**
     * 样本标准差 ： 每个值与平均值差的平方之和，除以N-1后的平方根
     * @param args
     */
    public static void main(String[] args) {
        Bag<Double> numbers = new Bag<>();
        while (!StdIn.isEmpty()){
            numbers.add(StdIn.readDouble());
        }
        int N = numbers.size();
        double sum = 0.0;
        for (double x : numbers) sum += x;
        double mean = sum / N;

        sum = 0.0;
        for (double x : numbers) sum += (x - mean) * (x - mean);

        double std = Math.sqrt(sum / (N - 1));

        StdOut.printf("Mean: %.2f\n", mean);
        StdOut.printf("Std dev: %.2f\n", std);
    }
}
